package zhuangtai;

// 电梯状态接口
interface ElevatorState {
    void openDoor();
    void closeDoor();
    void goUp();
    void goDown();
    void stop();
    void repair();
}

// 空闲状态
class IdleState implements ElevatorState {
    private Elevator elevator;

    public IdleState(Elevator elevator) {
        this.elevator = elevator;
    }

    @Override
    public void openDoor() {
        System.out.println("电梯门打开");
        elevator.setState(elevator.getDoorOpenState());
    }

    @Override
    public void closeDoor() {
        System.out.println("电梯门已关闭，当前处于空闲状态");
    }

    @Override
    public void goUp() {
        System.out.println("电梯开始上升");
        elevator.setState(elevator.getRunningState());
    }

    @Override
    public void goDown() {
        System.out.println("电梯开始下降");
        elevator.setState(elevator.getRunningState());
    }

    @Override
    public void stop() {
        System.out.println("电梯已经处于空闲状态，无需停止");
    }

    @Override
    public void repair() {
        System.out.println("电梯进入故障状态");
        elevator.setState(elevator.getFaultState());
    }
}

// 运行状态
class RunningState implements ElevatorState {
    private Elevator elevator;

    public RunningState(Elevator elevator) {
        this.elevator = elevator;
    }

    @Override
    public void openDoor() {
        System.out.println("警告：电梯运行中，不能开门！");
    }

    @Override
    public void closeDoor() {
        System.out.println("电梯运行中，门已关闭");
    }

    @Override
    public void goUp() {
        System.out.println("电梯继续上升");
    }

    @Override
    public void goDown() {
        System.out.println("电梯继续下降");
    }

    @Override
    public void stop() {
        System.out.println("电梯停止，到达第" + elevator.getCurrentFloor() + "层");
        elevator.setState(elevator.getIdleState());
    }

    @Override
    public void repair() {
        System.out.println("紧急停止！电梯进入故障状态");
        elevator.setState(elevator.getFaultState());
    }
}

// 开门状态
class DoorOpenState implements ElevatorState {
    private Elevator elevator;

    public DoorOpenState(Elevator elevator) {
        this.elevator = elevator;
    }

    @Override
    public void openDoor() {
        System.out.println("电梯门已打开");
    }

    @Override
    public void closeDoor() {
        System.out.println("电梯门正在关闭");
        elevator.setState(elevator.getDoorClosedState());
    }

    @Override
    public void goUp() {
        System.out.println("警告：电梯门未关闭，不能运行！");
    }

    @Override
    public void goDown() {
        System.out.println("警告：电梯门未关闭，不能运行！");
    }

    @Override
    public void stop() {
        System.out.println("电梯门已打开，无需停止");
    }

    @Override
    public void repair() {
        System.out.println("电梯进入故障状态，正在关闭门");
        elevator.setState(elevator.getFaultState());
    }
}

// 关门状态
class DoorClosedState implements ElevatorState {
    private Elevator elevator;

    public DoorClosedState(Elevator elevator) {
        this.elevator = elevator;
    }

    @Override
    public void openDoor() {
        System.out.println("电梯门正在打开");
        elevator.setState(elevator.getDoorOpenState());
    }

    @Override
    public void closeDoor() {
        System.out.println("电梯门已关闭");
    }

    @Override
    public void goUp() {
        System.out.println("电梯开始上升");
        elevator.setState(elevator.getRunningState());
    }

    @Override
    public void goDown() {
        System.out.println("电梯开始下降");
        elevator.setState(elevator.getRunningState());
    }

    @Override
    public void stop() {
        System.out.println("电梯已停止，门已关闭");
        elevator.setState(elevator.getIdleState());
    }

    @Override
    public void repair() {
        System.out.println("电梯进入故障状态");
        elevator.setState(elevator.getFaultState());
    }
}

// 故障状态
class FaultState implements ElevatorState {
    private Elevator elevator;

    public FaultState(Elevator elevator) {
        this.elevator = elevator;
    }

    @Override
    public void openDoor() {
        System.out.println("故障状态：无法开门，请等待维修");
    }

    @Override
    public void closeDoor() {
        System.out.println("故障状态：无法关门，请等待维修");
    }

    @Override
    public void goUp() {
        System.out.println("故障状态：无法上升，请等待维修");
    }

    @Override
    public void goDown() {
        System.out.println("故障状态：无法下降，请等待维修");
    }

    @Override
    public void stop() {
        System.out.println("电梯已因故障停止");
    }

    @Override
    public void repair() {
        System.out.println("维修完成，电梯恢复正常");
        elevator.setState(elevator.getIdleState());
    }
}

// 电梯类
class Elevator {
    private int currentFloor;
    private ElevatorState state;
    private ElevatorState idleState;
    private ElevatorState runningState;
    private ElevatorState doorOpenState;
    private ElevatorState doorClosedState;
    private ElevatorState faultState;

    public Elevator() {
        this.currentFloor = 1;
        this.idleState = new IdleState(this);
        this.runningState = new RunningState(this);
        this.doorOpenState = new DoorOpenState(this);
        this.doorClosedState = new DoorClosedState(this);
        this.faultState = new FaultState(this);
        this.state = idleState;
    }

    public void setState(ElevatorState state) {
        this.state = state;
    }

    public ElevatorState getState() {
        return state;
    }

    public ElevatorState getIdleState() {
        return idleState;
    }

    public ElevatorState getRunningState() {
        return runningState;
    }

    public ElevatorState getDoorOpenState() {
        return doorOpenState;
    }

    public ElevatorState getDoorClosedState() {
        return doorClosedState;
    }

    public ElevatorState getFaultState() {
        return faultState;
    }

    public int getCurrentFloor() {
        return currentFloor;
    }

    public void setCurrentFloor(int floor) {
        this.currentFloor = floor;
    }

    public void openDoor() {
        state.openDoor();
    }

    public void closeDoor() {
        state.closeDoor();
    }

    public void goUp() {
        if (currentFloor < 10) { // 假设最高10层
            currentFloor++;
            state.goUp();
        } else {
            System.out.println("已到达顶层，无法继续上升");
        }
    }

    public void goDown() {
        if (currentFloor > 1) { // 假设最低1层
            currentFloor--;
            state.goDown();
        } else {
            System.out.println("已到达底层，无法继续下降");
        }
    }

    public void stop() {
        state.stop();
    }

    public void repair() {
        state.repair();
    }
}